import { ref } from 'vue'
import { defineStore } from 'pinia'
import request from '@/api/request'
// 关于pinia的使用
export const useExamStore = defineStore('exam', () => {
    // 记录试卷分类
    const categories = ref<any>([])
    // 获取试卷分类数据
    async function getCategory() {
        await request.examCategoryList()
            .then(
                content => {
                    // console.log(content);
                    let { code, result } = content
                    if (code === 200) {
                        let newArr = result.map((item: any) => {
                            item.icon = request.host + item.icon;
                            return item;
                        })
                        categories.value = newArr;
                    }
                }
            )
    }


    // 定义试卷列表
    const exams = ref<any>([])
    async function getExams() {
        await request.examListAll()
            .then(
                content => {
                    // console.log(content);
                    let { code, result } = content;
                    if (code === 200) {
                        exams.value = result;
                    }
                }
            )
    }


    // 初始化
    getCategory();
    getExams();
    return {
        categories,
        exams,
        getCategory,
        getExams
    }

})